package com.ch3cooh.serviceuser.user.controller;

import com.ch3cooh.common.constant.GlobalConstants;
import com.ch3cooh.common.resp.ApiResult;
import com.ch3cooh.serviceuser.user.entity.User;
import com.ch3cooh.serviceuser.user.entity.UserElement;
import com.ch3cooh.serviceuser.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * @功能: 用户Controller
 * @作者:chanson
 * @时间:2018/5/24 21:59
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/login.json")
    public ApiResult<Void> login(@RequestBody @Valid User user , HttpSession session){
        UserElement ue = userService.login(user);
        if(ue != null){
            //重复登录，不刷新session过期时间
            if(session.getAttribute(GlobalConstants.REQUEST_USER_SESSION)!=null){
                session.setAttribute(GlobalConstants.REQUEST_USER_SESSION,ue);
            }
        }
        return ApiResult.success("登录成功");
    }

    @RequestMapping("/register.json")
    public ApiResult<Void> register(@RequestBody @Valid User user){
         userService.register(user);
         return ApiResult.success("注册成功");
    }

}
